package com.gxz.job.example.job;

import com.gxz.job.example.pojo.Order;
import com.gxz.job.example.service.OrderService;
import com.gxz.job.elasticjob.start.zookeeper.annotation.ElasticDataFlowJob;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.dataflow.DataflowJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Slf4j
//@Component
@ElasticDataFlowJob(name = "orderTimeoutJob",cron = "0/10 * * * * ?",overwrite = true
,shardingTotalCount = 3,streamingProcess = true)
public class OrderTimeoutJob implements DataflowJob<Order> {
    @Autowired
    private OrderService orderService;
    @Override
    public List<Order> fetchData(ShardingContext shardingContext) {
        //1.获取分片项
        int item = shardingContext.getShardingItem();
        //2.获取总分片项
        int totalCount = shardingContext.getShardingTotalCount();
        //3.基于分片项和总分片项，每次从订单表中抓取200个数据（超时订单）。
        List<Order> orderList = orderService.findOrderTimeOut(item, totalCount);
        return orderList;
    }

    @Override
    public void processData(ShardingContext shardingContext, List<Order> list) {
        //log.info(shardingContext.getShardingItem()+"分片项,"+list.size());
        for (Order order : list) {
            orderService.updateStatus(order,"4");
        }
    }
}
